package service;

import common.Pagination;
import common.StringUtils;
import dao.CroChangegoodsDao;
import dao.CroPurgoodsDao;
import dao.SysDictDao;
import entity.CroPurgoods;
import entity.SysDict;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vo.CroPurchaseVo;
import vo.CroPurgoodsVo;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wsx on 2017/5/10.
 */
@Service
public class CroPurgoodsService {
    @Autowired
    private CroPurgoodsDao croPurgoodsDao;
    @Autowired
    private SysDictDao sysDictDao;

    /*
    * 获取全部商品
    * **/
    @Transactional(readOnly = false)
    public List<CroPurgoodsVo> getPurgoods(){
        StringBuffer sb=new StringBuffer();
        sb.append("from CroPurgoods where del_flag='0'");
        return croPurgoodsDao.findByHqlExt(sb.toString(),CroPurgoodsVo.class);
    }

    /**
     * 通过ID查找商品
     * @param Id
     * @return
     */
    @Transactional(readOnly = false)
    public CroPurgoods getPurgoodsById(String Id){
        return croPurgoodsDao.getById(Id);
    }

     /*
    * 查询商品
    * **/
     @Transactional(readOnly = false)
     public List<CroPurgoodsVo> getPurgoodsByCondiction(String querygoodname,String querytype,String min_price,String max_price){
         StringBuffer sb=new StringBuffer();
         sb.append("from CroPurgoods where del_flag='0'");
             if (StringUtils.isNotBlank(querygoodname)) {
                 sb.append(" and goodname like'%" + querygoodname + "%'");
             }
             if (StringUtils.isNotBlank(querytype)&&!(querytype.equals("10"))) {
                 sb.append(" and type='" + querytype + "'");
             }
             if (StringUtils.isNotBlank(max_price) && StringUtils.isNotBlank(min_price)) {
                 sb.append(" and uniprice between '" + Double.parseDouble(min_price) + "' and '" + Double.parseDouble(max_price) + "'");
             } else if (StringUtils.isNotBlank(max_price)) {
                 sb.append(" and uniprice < '" + Double.parseDouble(max_price) + "'");
             } else if (StringUtils.isNotBlank(min_price)) {
                 sb.append(" and uniprice > '" + Double.parseDouble(min_price) + "'");
             }

         return croPurgoodsDao.findByHqlExt(sb.toString(),CroPurgoodsVo.class);
     }

        /*
    * 删除商品
    * **/
    @Transactional(readOnly = false)
    public Boolean deletePurgoods(String id){
        CroPurgoods croPurgoods=croPurgoodsDao.getById(id);
        croPurgoods.setDel_flag("1");
        croPurgoodsDao.update(croPurgoods);
        return true;
    }

    /**
     * 通过商品名字查找商品
     */
    @Transactional(readOnly = true)
    public CroPurgoods getPurgoodsByName(String name){
        Map<String,Object> map = new HashMap<>();
        map.put("goodname",name);
        List<CroPurgoods> list = croPurgoodsDao.findByProperty(map);
        if (list.size()==0){
            return null;
        }else {
            return list.get(0);

        }
    }

    /**
     * 代购辅助添加商品
     * @param croPurgoods 商品
     * @return 商品Id
     */
    @Transactional(readOnly = false)
    public String addPurgoods(CroPurgoods croPurgoods){
        return (String) croPurgoodsDao.save(croPurgoods);
    }


           /*
    * 添加商品
    * **/
    @Transactional(readOnly = false)
    public int  addPurgoods(CroPurgoods croPurgoods,String type,String goodname){
        int flag=0;
//        StringBuffer sb=new StringBuffer();
//        sb.append("from CroPurgoods where del_flag='0' and type='"+type+"' and goodname='"+goodname+"'");
//        List<CroPurgoodsVo> croPurgoodsVos=croPurgoodsDao.findByHqlExt(sb.toString(),CroPurgoodsVo.class);
//       if(croPurgoodsVos.size()>0){
//            flag=1;
//       }else {
//           croPurgoodsDao.save(croPurgoods);
//       }
        croPurgoodsDao.save(croPurgoods);
        return flag;
    }
    /*
* 修改商品
* **/
    @Transactional(readOnly = false)
    public Boolean updatePurgoods(CroPurgoods croPurgoods){
        croPurgoodsDao.update(croPurgoods);
        return true;
    }
    /**
     * 返回分页的商品信息
     *
     * @return list
     */
    @Transactional(readOnly = true)
    public Pagination<CroPurgoodsVo> listsPage(int index, int size) {
        StringBuilder hql = new StringBuilder();
        hql.append("from CroPurgoods where del_flag='0'");
        Pagination<CroPurgoodsVo> page;
        page = croPurgoodsDao.findPagination(hql,index,size);
        try {
            page.setItems( croPurgoodsDao.transEntity(page.getItems(), CroPurgoodsVo.class));
        } catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException | InstantiationException | NoSuchMethodException e) {
            e.printStackTrace();
        }
        return page;
    }

    /**
     * 返回按条件查询分页的商品信息
     *
     * @return list
     */
    @Transactional(readOnly = true)
    public Pagination<CroPurgoodsVo> listsPageByConditcion(int index, int size,String querygoodname,String querytype,String min_price,String max_price) {
        StringBuilder sb = new StringBuilder();
        sb.append("from CroPurgoods where del_flag='0'");
        if (StringUtils.isNotBlank(querygoodname)) {
            sb.append(" and goodname like'%" + querygoodname + "%'");
        }
        if (StringUtils.isNotBlank(querytype)&&!(querytype.equals("11"))) {
            sb.append(" and type='" + querytype + "'");
        }
        if (StringUtils.isNotBlank(max_price) && StringUtils.isNotBlank(min_price)) {
            sb.append(" and uniprice between '" + Double.parseDouble(min_price) + "' and '" + Double.parseDouble(max_price) + "'");
        } else if (StringUtils.isNotBlank(max_price)) {
            sb.append(" and uniprice < '" + Double.parseDouble(max_price) + "'");
        } else if (StringUtils.isNotBlank(min_price)) {
            sb.append(" and uniprice > '" + Double.parseDouble(min_price) + "'");
        }
        Pagination<CroPurgoodsVo> page;
        page = croPurgoodsDao.findPagination(sb.toString(),index,size);
        try {
            page.setItems( croPurgoodsDao.transEntity(page.getItems(), CroPurgoodsVo.class));
        } catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException | InstantiationException | NoSuchMethodException e) {
            e.printStackTrace();
        }
        return page;
    }
    /*
    * 获取修改商品的信息
    * */
    @Transactional(readOnly = true)
    public CroPurgoodsVo getUpdateGoodsInfo(String id){
        String hql = "from CroPurgoods where id=?";
      return (CroPurgoodsVo) croPurgoodsDao.findByHqlExt(hql, CroPurgoodsVo.class, id).get(0);
    }
    @Transactional(readOnly = false)
    public Boolean addGoodsType(SysDict sysDict){
        croPurgoodsDao.save(sysDict);
        return true;
    }

    /**
     * 前台查询
     * @return list
     */
    @Transactional(readOnly = true)
    public Pagination<CroPurgoodsVo> frontListsPageByConditcion(int index,int size,String search) {
        StringBuilder sb = new StringBuilder();
        sb.append("from CroPurgoods where del_flag='0'");
        if(StringUtils.isNotBlank(search)){
            sb.append(" and goodname like'%" + search + "%'");
        }
        Pagination<CroPurgoodsVo> page;
        page = croPurgoodsDao.findPagination(sb.toString(),index,size);
        try {
            page.setItems( croPurgoodsDao.transEntity(page.getItems(), CroPurgoodsVo.class));
        } catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException | InstantiationException | NoSuchMethodException e) {
            e.printStackTrace();
        }
        return page;
    }

    /**
     * 根据id得到商品的实体
     * */
    @Transactional(readOnly = false)
    public CroPurgoods getEntityGoods(String id){
        return croPurgoodsDao.getById(id);
    }
    /*
    * 修改商品类型
    * */
    @Transactional(readOnly = false)
    public Boolean updateGoodstype(SysDict sysDict){
        croPurgoodsDao.update(sysDict);
        return true;
    }
    /*
    * 获取商品类型信息
    * */
    @Transactional(readOnly = false)
    public SysDict getSysDictinfo(String type){
        StringBuffer sb=new StringBuffer();
        sb.append("from SysDict where label='goods_type' and del_flag='0' and type='"+type+"'");
         List<SysDict> sysDictList=sysDictDao.findList(sb.toString());
        return sysDictList.get(0);
    }

}
